package com.aplink.dellcommercial.fragments;

import android.content.Intent;
import android.os.Bundle;

import com.aplink.dellcommercial.R;
import com.aplink.dellcommercial.activities.ChangePasswordActivity;
import com.aplink.dellcommercial.configuration.DellController;
import com.aplink.dellcommercial.database.DellDataTable;
import com.aplink.dellcommercial.util.DellCommercialUtility;
import com.aplink.generic.GenericFragment;
import com.aplink.generic.constant.SqlDataType;
import com.aplink.generic.database.SqlMethods;
import com.aplink.generic.pojo.GenericObject;
import com.aplink.generic.pojo.controlspec.ControlSpec;
import com.aplink.generic.pojo.controlspec.MenuSpec.MenuItem;

/**
 * Created by Hien Nguyen Le on 1/8/14.
 */
public class UserSettingFragment extends GenericFragment {

    @Override
    public int getFormDesign() {
        return R.raw.form_design_user_setting;
    }

    @Override
    public int getContentData() {
        return R.raw.content_data_get_user_profiles;
    }

    @Override
    public String getTable() {
        return DellDataTable.UserProfile.TABLE_NAME;
    }

    @Override
    public void onActivityCreated( Bundle savedInstanceState ) {
        super.onActivityCreated( savedInstanceState );
        String weblogin = DellController.getInstance().getAccountEmail();

        String whereClause = SqlMethods.getInstance().equal( SqlDataType.TEXT,
                DellDataTable.UserProfile.USER_WEB_LOGIN, weblogin ).toString();

        GenericObject genericObject = selectCommand().get( getObjPropCol(),
                whereClause );
        this.setPassedGenericObject( genericObject );
        this.initData();
        this.loadDataToForm();
    }

    @Override
    protected void onHandleControlClickEvent( ControlSpec controlSpec ) {

    }

    @Override
    public int getContentView() {
        return R.layout.aplink_dellcommercial_user_setting;
    }

    @Override
    protected void onHandleOptionMenuClickEvent( MenuItem menuItem ) {
        final String validationGroup = menuItem.getValidationgroup();
        if ( validationGroup.equalsIgnoreCase( "change_password" ) ) {
            Intent intent = new Intent( getApplicationContext(),
                    ChangePasswordActivity.class );
            startActivity( intent );
        } else if ( validationGroup.equalsIgnoreCase( "log_out" ) ) {
            DellCommercialUtility.logout( getActivity() );
        }
    }
}
